// pages/jiuai/index.js
var demo = require("../../utils/util");
var amapFile = require('../../libs/amap-wx.js');//如：..­/..­/libs/amap-wx.js
var markersData = {
  latitude: '',//纬度
  longitude: '',//经度
  key: "bb88116c8d8ae2c6b9d28755c090cace"//申请的高德地图key
};

Page({
  /**
   * 页面的初始数据
   */
  data: {
    backgroundImage:'',//背景图
      city:'',//城市
      time:demo.formatDate(new Date()),//时间
      winddirection:'',//风向
      windpower:'',//风力级别
      weather:'',//天气
      temperature:'',//气温
      humidity:'',//空气湿度
      reporttime:'',//发布时间
      chuanyiLevel:'',//穿衣指数level
      chuanyiDesc:'',//穿衣指数desc
      xicheLevel:'',//洗车指数level
      xicheDesc:'',//洗车指数desc
      ganmaoLevel:'',//感冒指数level
      ganmaoDesc:'',//感冒指数desc
      yundongLevel:'',//运动指数level
      yundongDesc:'',//运动指数desc
      ziwaixianLevel:'',//紫外线强度指数level
      ziwaixianDesc:'',//紫外线强度指数desc
      inputcity:'',
      input:'',
      inputValue: null,
  },
  getWeatherIcon(weather){
    switch(weather){
        case "晴":
           return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.588ku.com%2Fback_pic%2F05%2F86%2F88%2F805c853ab4b37e9.jpg&refer=http%3A%2F%2Fbpic.588ku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1628995186&t=1fccbbf4b870950014fcc633f66e75b9';
        case "阴":
          return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn4-q.mafengwo.net%2Fs5%2FM00%2FC4%2F88%2FwKgB3FF0CH2AAHrKAASnlxnZlOM89.jpeg%3FimageMogr2%2Fstrip&refer=http%3A%2F%2Fn4-q.mafengwo.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629007644&t=28a2cde248c6fa9bf5f0871c8d5f8250';
          case "多云":
          return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-vo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F42a98226cffc1e17678dbc364590f603738de983.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629003349&t=e2601b87c681927bbb6beefee16959ab';
          case "小雨":
            return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fphoto2%2F1654142%2F1121046706926_mthumb.JPG&refer=http%3A%2F%2Fimg.pconline.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629003302&t=2e288fa77aee06b92e96d4be51929b5a';
          case "中雨":
            return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180827%2Fb9a4ca2454af4c0cb3d9e66cb66cd7b5.gif&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629008022&t=34602d4ae27283a0ed376e0807c73f2c';    
          case "大雨":
            return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Ftranslate%2F320%2Fw640h480%2F20180727%2FwFrq-hfvkitx8454406.gif&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1628997356&t=08f568090595501998b9f5e233dbcb0f';
          case "沙尘暴":
            return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic9.tianqijun.com%2Fupload%2Farticle%2F16-04-06%2FnFGX_160406072426_5.png&refer=http%3A%2F%2Fpic9.tianqijun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629009177&t=b928be489c0c3ddcacbefaecd7c4c34e';
          case "雪":
            return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fq_70%2Cc_zoom%2Cw_640%2Fimages%2F20180105%2Fc70e784ba2ea47cea5885e3bfbba2717.gif&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629008988&t=53d1588f06e9563af6f946d737a8ac3f';
          case "雾":
            return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhuafans.dbankcloud.com%2Fpic%2F2017%2F11%2F10%2F721089ddd92def423746f68e1b26e933_IMG_20171110_071855.jpg%3Fmode%3Ddownload&refer=http%3A%2F%2Fhuafans.dbankcloud.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629009075&t=4e904ddd27048c0cc82530781e4ca807';
          case "龙卷风":
            return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F1a24bb2624739be9b3705363c72245bd3a9974a39773f-ZhmnT8_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629009778&t=1c165ea50135b83c1a04c61469e2aab4';
          }
  },
  value(event){
    this.setData({
      inputcity:event.detail.value,
      'inputValue': ''
    })
    var that = this;
    var myAmapFun = new amapFile.AMapWX({key:markersData.key});
    myAmapFun.getWeather({
      type:'live',
      city:event.detail.value,
      success: function(data){
        //成功回调
        console.log(data)
        if(demo.ccc(event.detail.value)==false){
          that.setData({
            city:data.city.data,
            weather:data.weather.data,
            temperature:data.temperature.data,
            winddirection:data.winddirection.data,
            humidity:data.humidity.data,
            windpower:data.windpower.data,
            backgroundImage:that.getWeatherIcon(data.weather.data)
            // time:demo.formatTime(new Date(),"M-D")
          })
        }
      },
      fail: function(info){
        wx.showModal({
          title: '提示',
          content: '请输入正确的城市名',
          success: function (res) {
            if (res.confirm) {//这里是点击了确定以后
              console.log('用户点击确定')
            } else {//这里是点击了取消以后
              console.log('用户点击取消')
            }
          }
        })
      }
    });
    myAmapFun.getWeather({
      type:'forecast',//表示获取的是预报信息 默认live（实时天气）
     city:event.detail.value,
      success: function(data){
        console.log(data)
        //成功回调
        if(demo.ccc(event.detail.value)==false){
          var weatherarr = new Array(4);
          for(var i = 0;i<4; i++){
            weatherarr[i] = data.forecast.casts[i];//获取后三天预报信息 今日的不保存
          }
          that.setData({
            forecast:weatherarr,
            city:data.forecast.city
          })
        }
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    }),
    wx.request({
      url: 'https://www.tianqiapi.com/life/lifepro?appid=32866273&appsecret=PTR1JmmA',
      data: {
       city:event.detail.value
      },
      header: { 'content-type': 'application/json' },
      success: function(res) {
       // 收到https服务成功后返回
       console.log(11111,res);
       if(!res.data.city || event.detail.value=='abc' || demo.ccc(event.detail.value)==true){
         wx.showModal({
          title: '提示',
          content: '请输入正确的城市名',
          success: function (res) {
            if (res.confirm) {//这里是点击了确定以后
              console.log('用户点击确定')
            } else {//这里是点击了取消以后
              console.log('用户点击取消')
            }
          }
        })
       }else{
        that.setData({
          chuanyiDesc:res.data.data.chuanyi.desc,//穿衣指数level
          chuanyiLevel:res.data.data.chuanyi.level,//穿衣指数desc
          xicheLevel:res.data.data.xiche.level,//洗车指数level
          xicheDesc:res.data.data.xiche.desc,//洗车指数desc
          ganmaoLevel:res.data.data.ganmao.level,//感冒指数level
          ganmaoDesc:res.data.data.ganmao.desc,//感冒指数desc
          yundongLevel:res.data.data.yundong.level,//运动指数level
          yundongDesc:res.data.data.yundong.desc,//运动指数desc
          ziwaixianLevel:res.data.data.ziwaixian.level,//紫外线强度指数level
          ziwaixianDesc:res.data.data.ziwaixian.desc,//紫外线强度指数desc
         })
       }
      },
      fail: function() {
       // 发生网络错误等情况触发
        wx.showModal({
          title: '提示',
          content: '请输入正确的城市名',
          success: function (res) {
            if (res.confirm) {//这里是点击了确定以后
              console.log('用户点击确定')
            } else {//这里是点击了取消以后
              console.log('用户点击取消')
            }
          }
        })
      },
      complete: function(res) {
       // 成功或者失败后触发
      }
      })
  },
  onLoad: function() {
    wx.onNetworkStatusChange(function(res){
      console.log(res)
      if(res.networkType == "none"){
        wx.showLoading({
          title: '无网络....',
        })
      }else{
        wx.showLoading({
          title: '连接中.....',
        })
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        this.getData();
      }
    })
    var that = this;
    var myAmapFun = new amapFile.AMapWX({key:markersData.key});
    myAmapFun.getWeather({
      type:'live',//实时天气预报
      city:'',
      success: function(data){
        //成功回调
        console.log(data)
        that.setData({
          city:data.city.data,
          weather:data.weather.data,
          temperature:data.temperature.data,
          winddirection:data.winddirection.data,
          humidity:data.humidity.data,
          windpower:data.windpower.data,
          reporttime:data.liveData.reporttime,
          backgroundImage:that.getWeatherIcon(data.weather.data)
        });
        myAmapFun.getWeather({
          type:'forecast',//表示获取的是预报信息 默认live（实时天气）
          success: function(data){
            console.log(data)
            //成功回调
            var weatherarr = new Array(4);
            for(var i = 0; i < 4; i++){
              weatherarr[i] = data.forecast.casts[i];//获取今天以及后三天预报信息 今日的不保存
            }
            that.setData({
              forecast:weatherarr,
             city:data.forecast.city
            })
          },
          fail: function(info){
            //失败回调
            console.log(info)
          }
        });
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    });
    wx.request({
      url: 'https://www.tianqiapi.com/life/lifepro?appid=32866273&appsecret=PTR1JmmA',
      data: {
      },
      header: { 'content-type': 'application/json' },
      success: function(res) {
       // 收到https服务成功后返回
       console.log(res.data);
       that.setData({
        chuanyiDesc:res.data.data.chuanyi.desc,//穿衣指数level
        chuanyiLevel:res.data.data.chuanyi.level,//穿衣指数desc
        xicheLevel:res.data.data.xiche.level,//洗车指数level
        xicheDesc:res.data.data.xiche.desc,//洗车指数desc
        ganmaoLevel:res.data.data.ganmao.level,//感冒指数level
        ganmaoDesc:res.data.data.ganmao.desc,//感冒指数desc
        yundongLevel:res.data.data.yundong.level,//运动指数level
        yundongDesc:res.data.data.yundong.desc,//运动指数desc
        ziwaixianLevel:res.data.data.ziwaixian.level,//紫外线强度指数level
        ziwaixianDesc:res.data.data.ziwaixian.desc,//紫外线强度指数desc
       })
      },
      fail: function() {
       // 发生网络错误等情况触发
      },
      complete: function(res) {
       // 成功或者失败后触发
      }
      })
  },
    //刷新
    onRefresh(){
      //在当前页面显示导航条加载动画
      wx.showNavigationBarLoading(); 
      //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
      wx.showLoading({
        title: '刷新中...',
      })
      this.getData();
    },
  //网络请求，获取数据
  getData(){
    var that = this;
    var myAmapFun = new amapFile.AMapWX({key:markersData.key});
    myAmapFun.getWeather({
      type:'live',
      city:'',
      success: function(data){
        //成功回调
                      //隐藏loading 提示框
              wx.hideLoading();
              //隐藏导航条加载动画
              wx.hideNavigationBarLoading();
              //停止下拉刷新
              wx.stopPullDownRefresh();
        console.log(data)
        that.setData({
          city:data.city.data,
          weather:data.weather.data,
          temperature:data.temperature.data,
          winddirection:data.winddirection.data,
          humidity:data.humidity.data,
          windpower:data.windpower.data,
          reporttime:data.liveData.reporttime,
          backgroundImage:that.getWeatherIcon(data.weather.data)
        })
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    });
    wx.request({
      url: 'https://www.tianqiapi.com/life/lifepro?appid=32866273&appsecret=PTR1JmmA',
      data: {
      },
      header: { 'content-type': 'application/json' },
      success: function(res) {
       // 收到https服务成功后返回
       console.log(res.data);
       that.setData({
        chuanyiDesc:res.data.data.chuanyi.desc,//穿衣指数level
        chuanyiLevel:res.data.data.chuanyi.level,//穿衣指数desc
        xicheLevel:res.data.data.xiche.level,//洗车指数level
        xicheDesc:res.data.data.xiche.desc,//洗车指数desc
        ganmaoLevel:res.data.data.ganmao.level,//感冒指数level
        ganmaoDesc:res.data.data.ganmao.desc,//感冒指数desc
        yundongLevel:res.data.data.yundong.level,//运动指数level
        yundongDesc:res.data.data.yundong.desc,//运动指数desc
        ziwaixianLevel:res.data.data.ziwaixian.level,//紫外线强度指数level
        ziwaixianDesc:res.data.data.ziwaixian.desc,//紫外线强度指数desc
       })
      },
      fail: function() {
       // 发生网络错误等情况触发
      },
      complete: function(res) {
       // 成功或者失败后触发
      }
      }) 
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      //调用刷新时将执行的方法
    this.onRefresh();
  },

  // /**
  //  * 生命周期函数--监听页面加载
  //  */
  // onLoad: function (options) {

  // },

  // /**
  //  * 生命周期函数--监听页面初次渲染完成
  //  */
  // onReady: function () {

  // },

  // /**
  //  * 生命周期函数--监听页面显示
  //  */
  // onShow: function () {

  // },

  // /**
  //  * 生命周期函数--监听页面隐藏
  //  */
  // onHide: function () {

  // },

  // /**
  //  * 生命周期函数--监听页面卸载
  //  */
  // onUnload: function () {

  // },

  // /**
  //  * 页面相关事件处理函数--监听用户下拉动作
  //  */
  // onPullDownRefresh: function () {

  // },

  // /**
  //  * 页面上拉触底事件的处理函数
  //  */
  // onReachBottom: function () {

  // },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {

  // }

  onShareAppMessage: function () {
    const { info, code } = that.data;
    return {
      title: info.name,
      path: `/pages/share/index?code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  },
  onShareTimeline: function(){
    const { info, code } = that.data;
    return {
      title: info.name,
      query: `code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  }
})
